package OOday03;

/**
 * 多态的演示
 */
public class PolymorphicDemo {
    public static void main(String[] args) {
        Animal o=new Dog("小狗",1,"白色","骨头");
        Dog g=(Dog)o;
        Swim s =(Swim)o ;
        //Fish f=(Fish) o;//类型转换异常
        System.out.println(o instanceof Dog);//true
        System.out.println(o instanceof Swim);//true
        System.out.println(o instanceof Fish);//false
        if (o instanceof Fish){
            Fish f=(Fish) o;
        }else {
            System.out.println("o不是Fish类型");
        }



















//        Master master=new Master();
//        Dog dg=new Dog("大黄",2,"白色","骨头");
//        Chick ck =new Chick("战斗机",2,"黑色");
//        Fish fh=new Fish("虎鲨",2,"白色");
//        master.feed(dg);
//        master.feed(ck);
//        master.feed(fh);

        Animal[] animal=new Animal[5];
        animal[0]=new Dog("小黑",1,"白色","骨头");
        animal[1]=new Dog("大黄",3,"黄色","骨头");
        animal[2]=new Chick("小鸡",1,"花色");
        animal[3]=new Chick("小工鸡",1,"白色");
        animal[4]=new Fish("小<・)))><<",4,"黑色");
        for (int i=0;i<animal.length;i++){
            System.out.println(animal[i].name);
            animal[i].eat();
            if (animal[i] instanceof Dog){
                Dog dog=(Dog)animal[i];
                dog.eat();
            }
            if (animal[i]instanceof Chick){
                Chick chick=(Chick) animal[i];
                chick.layEggs();
            }
            if (animal[i]instanceof Swim){
                Swim w=(Swim) animal[i];
                w.swim();
            }
        }















        /**Dog dg=new Dog("大炮",2,"黑色","shi");
        dg.eat();
        dg.lookhome();
        dg.swim();
        Animal aa=new Dog("飞机",1,"白色","骨头");
        aa.eat();
        aa.drink();
        //aa.lookhome();向上造型只能调用超类和子类共有的。
        //aa.swim();**/

    }
}
